import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
import { useCallback, useMemo } from 'react';
import { useRequest } from 'umi';
import { ConditionManageAPI } from '../common/type';
import { getSearchColumns } from '../mconfig';

let baseUrl = getBaseUrl();
const conditionTableManage = (curRes: Record<string, any>) => {
  /**
   * 获取列表数据
   */
  const { tableProps, data, error, loading, run, params } = useRequest(
    (opt: ConditionManageAPI.IGridRequestType) => {
      let personUrl = `${baseUrl}/activitiController/conditionDataGrid`;
      let personBody: any = {
        current: 1,
        pageSize: 10,
        sort: 'tableName',
        order: 'desc',
        ...opt,
      };
      return requestFunComponFiex('POST', personUrl, personBody);
    },
    {
      manual: true, //页面初始请求为true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getConditionTableData = useCallback(
    (otherRequest: ConditionManageAPI.IGridRequestType | any) => {
      const request: ConditionManageAPI.IGridRequestType | any = {
        ...otherRequest,
      };

      run({
        ...request,
      });
    },
    [],
  );

  /* 页面列表 */
  let tableValues = useMemo(() => {
    return {
      conditionData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /* 数据字典值  */
  let loadMapList = useMemo(() => {
    return {
      loadMdicMapList: [],
      searchList: getSearchColumns(),
    };
  }, [params]);

  return {
    loading,
    getConditionTableData, //其他请求操作方法例如刷新等等
    // manageTableParams,
    otherParams: params?.[0],
    ...tableValues, //页面列表值
    ...loadMapList,
  };
};

export { conditionTableManage };
